/**
 * Project Name:kany-utils
 * File Name:IPUtils.java
 * Package Name:me.kany.utils.ip
 * Date:2016年4月17日上午11:44:33
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.utils.iputils;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:IPUtils <br/>
 * Function: 通过用户的请求获取真实的Ip地址. <br/>
 * Date:     2016年4月17日 上午11:44:33 <br/>
 * @author   Jason.Wang
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
public class IPUtils {
	
	/**
	 * 通过用户的请求获取用户的ip地址和主机信息
	 * @param request 用户发送的请求
	 * @return String 返回类型  （地址跳转参数）
	 * @throws Exception
	 */
	public static String getClientIpAddr(HttpServletRequest request) {
		String ipAddress = request.getHeader("x-forwarded-for");
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getHeader("Proxy-Client-IP");
			System.out.println(ipAddress);
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
			System.out.println(ipAddress);
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
			System.out.println(ipAddress);
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getHeader("HTTP_CLIENT_IP");
			System.out.println(ipAddress);
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getRemoteAddr();
			System.out.println(ipAddress);
		}
		String ip = ipAddress;
		return ip;
    }
	
	/**
	 * 
	 * getIPSeeker:根据传递的IP地址获取IP信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param ipAddress
	 * @return
	 * @since JDK 1.7
	 */
	public static IPLocation getIPSeeker(String ipAddress) {
		IPSeeker ipSeeker = new IPSeeker("QQWry.Dat", "./ipdata");
		return ipSeeker.getIPLocation(ipAddress);
	}
}
